from pygame.sprite import *
import pygame

from Config import Config


class Cell(Sprite):
    # 该点是否有地雷
    __valueNone = 0
    __valueMine = 1

    # 该点的操作情况
    __statusIntact = 0
    __statusMarked = 1
    __statusClicked = 2

    __value = 0
    __status = 0
    __mineNum = 0

    __images = []
    __bgColor = None
    __size = 0
    __x = 0
    __y = 0
    __numColor = {}

    def __init__(self, size, x, y, bgColor, *groups):
        super().__init__(*groups)
        self.__status = self.__statusIntact
        self.__value = self.__valueNone
        self.__bgColor = bgColor
        self.__size = size
        self.__x = x
        self.__y = y

        self.__numColor[1] = Config.BLUE1
        self.__numColor[2] = Config.GREEN1
        self.__numColor[3] = Config.RED1
        self.__numColor[4] = Config.BLUE2
        self.__numColor[5] = Config.RED2
        self.__numColor[6] = Config.GREEN1
        self.__numColor[7] = Config.BLUE2
        self.__numColor[8] = Config.RED2

    def mined(self):
        return self.__value == self.__valueMine

    def mine(self):
        self.__value = self.__valueMine

    def value(self):
        return self.__value

    def click(self):
        if self.__status == self.__statusIntact:
            self.__status = self.__statusClicked

    def mark(self):
        if self.__status == self.__statusMarked:
            self.__status = self.__statusIntact
        elif self.__status == self.__statusIntact:
            self.__status = self.__statusMarked

    def marked(self):
        return self.__status == self.__statusMarked

    def isEmpty(self):
        return self.__status != self.__statusMarked \
               and self.__value == self.__valueNone \
               and self.__mineNum <= 0

    def incMineNum(self):
        self.__mineNum += 1

    def paint(self, window):
        rect = (self.__x * self.__size, self.__y * self.__size, self.__size, self.__size)
        txtColor = Config.RED
        bgColor = self.__bgColor
        text = None

        if self.__status == self.__statusClicked:
            if self.__value == self.__valueNone:
                if self.__mineNum > 0:
                    txtColor = self.__numColor[self.__mineNum]
                    text = '%d' % self.__mineNum
            elif self.__value == self.__valueMine:
                text = '雷'
        elif self.__status == self.__statusMarked:
            bgColor = Config.BLUE1
            txtColor = Config.WHITE
            text = '疑'
        else:
            bgColor = Config.BLUE1

        if bgColor:
            pygame.draw.rect(window, bgColor, rect)
        if text:
            font = pygame.font.SysFont(Config.GLOBAL_FONT, Config.CELL_TXT_SIZE)
            surface = font.render(text, True, txtColor)
            marginTop = int((self.__size - surface.get_height()) / 2)
            marginLeft = int((self.__size - surface.get_width()) / 2)
            point = (self.__x * self.__size + marginLeft, self.__y * self.__size + marginTop)
            window.blit(surface, point)

    def __str__(self):
        return '(%d,%d,%d)' % (self.__value, self.__status, self.__mineNum)
